package com.intco.ait.quotation.api.common.model;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * author: Angus
 */
@Builder
@Getter
@Setter
@ToString
public class BaseResponse<T> implements Serializable {

    private static final long serialVersionUID = -4335835578133862575L;

    /**
     * 返回码
     */
    private int code;

    /**
     * 数据
     */
    private T data;

    /**
     * 返回信息描述
     */
    private String msg;

    public BaseResponse(int code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    public BaseResponse() {

    }

    public static <T> BaseResponse<T> success(T data, String msg) {
        return new BaseResponse<>(HttpStatus.OK.value(), data, msg);
    }

    public static <T> BaseResponse<T> success(T data) {
        return success(data, "");
    }

    public static <T> BaseResponse<T> success(String msg) {
        return success(null, msg);
    }

    public static <T> BaseResponse<T> success() {
        return success(null);
    }

    public static <T> BaseResponse<T> fail(String msg) {
        return new BaseResponse<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), null, msg);
    }

    public static <T> BaseResponse<T> fail(T data, String msg) {
        return new BaseResponse<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), data, msg);
    }

    public static <T> BaseResponse<T> fail() {
        return fail(null);
    }

    public static <T> BaseResponse<T> fail(int code, String message) {
        return new BaseResponse<>(code, null, message);
    }
}
